--[[
    交易行与服务器交互的网络通信类 
--]]

require "Common/define"
require "Logic/Config"
require "Net/send_Msg"
require "Logic/BrokerManager"
require "proto/broker/message/ReqBrokerGoodsBuyMessage"
require "proto/broker/message/ReqBrokerGoodsListMessage"
require "proto/broker/message/ReqBrokerGoodsRegisterMessage"
require "proto/broker/message/ReqBrokerPetEvolutionMessage"
require "proto/broker/message/ResBrokerGoodsBuyMessage"
require "proto/broker/message/ResBrokerGoodsListMessage"
require "proto/broker/message/ResBrokerGoodsRegisterMessage"
require "proto/broker/message/ResBrokerPetEvolutionMessage"
require "proto/broker/message/ReqBrokerGoodsUnregisterMessage"

BrokerHandler = BrokerHandler or {}
local this = BrokerHandler

--交易行第一页签 显示的寄售宠物列表
function BrokerHandler.ReqBrokerGoodsList(in_BrokerType,in_page)
    local _req = ReqBrokerGoodsListMessage.New()
    _req.brokerType = in_BrokerType
    _req.page = in_page
    sendReqMessage(_req)
end
function BrokerHandler.ResBrokerGoodsList(in_message)
    if in_message.state == 0 then
        BrokerManager.BrokerPetType = tonumber(in_message.brokerType)
        local _hasNextPage = BrokerManager.GetPerPageMaxItem() == #in_message.goods
        local _eventName
        if BrokerManager.BrokerPetType == BrokerUtils.BrokerPetType.Myself then
            BrokerManager.MyConsignmentList = {}
            _eventName = Config.EventType.PETSHOP_RefreshMyConsignmentList
        elseif BrokerManager.BrokerPetType == BrokerUtils.BrokerPetType.Buy then
            if tonumber(in_message.page) == 1 then
                BrokerManager.BrokerSellPetList = {}
            end
            _eventName = Config.EventType.PETSHOP_RefreshBrokerGoodsList
            BrokerManager.BrokerSellPetPage = tonumber(in_message.page)
            BrokerManager.HasBrokerSellNextPage = _hasNextPage
        elseif BrokerManager.BrokerPetType == BrokerUtils.BrokerPetType.Evolution then
            if tonumber(in_message.page) == 1 then
                BrokerManager.BrokerEvolutionPetList = {}
            end
            _eventName = Config.EventType.PETSHOP_RefreshBrokerEvolutionList
            BrokerManager.BrokerEvolutionPetPage = tonumber(in_message.page)
            BrokerManager.HasBrokerEvolutionNextPage = _hasNextPage
        end
       
        --BrokerGoodsBean
        for _, brokerGoodsBean in ipairs(in_message.goods) do
            if brokerGoodsBean.type == BrokerUtils.PetGoodsType then
                BrokerManager.AddBrokerPet(brokerGoodsBean,tonumber(in_message.brokerType))
            end
        end
        BrokerManager.SortListByBrokerType(tonumber(in_message.brokerType))
        FireEvent(_eventName)
    else
        BrokerHandler.OnShowTips(in_message.state)
    end
end

-- 交易行第三页签 自己的宠物上架寄售(仅限宠物)
function BrokerHandler.ReqBrokerGoodsRegister(in_petId,in_sellPrice,in_evolutionPrice)
    this.addedPetId = in_petId
    BrokerManager.canShopPet = nil
    local _req = ReqBrokerGoodsRegisterMessage.New()
    _req.goodsType = BrokerUtils.PetGoodsType
    _req.uid = in_petId
    _req.sellPrice = in_sellPrice
    _req.evolutionPrice = in_evolutionPrice
    sendReqMessage(_req)
end
function BrokerHandler.ResBrokerGoodsRegister(in_message)
    BrokerManager.canShopPet = true
    if in_message.state == 0 then
        PetManager.RemovePetByPetId(this.addedPetId)
        BrokerManager.BrokerAddedPetId = 0
        BrokerManager.IsEvolutionSelected = false
        BrokerManager.IsSellSelected = false
        BrokerHandler.ReqBrokerGoodsList(BrokerUtils.BrokerPetType.Buy,1)
        BrokerHandler.ReqBrokerGoodsList(BrokerUtils.BrokerPetType.Evolution,1)
        BrokerHandler.ReqBrokerGoodsList(BrokerUtils.BrokerPetType.Myself,1)
        if #in_message.addItems ~= 0 then
            logicMgr.UIShowManager.ShowRewardByTempItemInfo(in_message.addItems)           
        end
        FireEvent(Config.EventType.RefreshPetFollow)
    else
        BrokerHandler.OnShowTips(in_message.state)
    end
    this.addedPetId = 0
end

-- 交易行第一页签 购买他人上架的宠物
function BrokerHandler.ReqBrokerGoodsBuy(in_goodsId)
    local _req = ReqBrokerGoodsBuyMessage.New()
    _req.goodsId = in_goodsId
    sendReqMessage(_req)
end
function BrokerHandler.ResBrokerGoodsBuy(in_message)
    if in_message.state == 0 then
        closeUI(PanelNames.UIPetShopPetDetail)
        BrokerHandler.ReqBrokerGoodsList(BrokerUtils.BrokerPetType.Buy,1)
    else
        if in_message.state == 4086 then
            FireEvent(Config.EventType.RenovePetChild)
        end
        BrokerHandler.OnShowTips(in_message.state)
    end
end

--交易行第一页签 与他人上架的宠物进化
function BrokerHandler.ReqBrokerPetEvolution(in_goodsId,in_myEvolutionPetId,in_lockParts)
    this.myEvolutionPetId = in_myEvolutionPetId
    local _req = ReqBrokerPetEvolutionMessage.New()
    _req.goodsId = in_goodsId
    _req.petId = in_myEvolutionPetId
    _req.lockParts = in_lockParts
    sendReqMessage(_req)
end
function BrokerHandler.ResBrokerPetEvolution(in_message)
    if in_message.state == 0 then
        PetManager.lockParts = {}
        closeUI(PanelNames.Page_PetApplyMyRequest)
        closeUI(PanelNames.UIPetShopPetDetail)
        PetManager.RemovePetByPetId(this.myEvolutionPetId)
        BrokerManager.BrokerAddedPetId = 0
        BrokerHandler.ReqBrokerGoodsList(BrokerUtils.BrokerPetType.Evolution,1)
    else
        if in_message.state == 4086 then
            FireEvent(Config.EventType.RenovePetChild)
        end
        BrokerHandler.OnShowTips(in_message.state)
    end
    this.myEvolutionPetId = 0
end
--下架
function BrokerHandler.ReqUnRegisterPet(goodId)
    local req =  ReqBrokerGoodsUnregisterMessage.New()
    req.goodsId = goodId
    sendReqMessage(req)
end
function BrokerHandler.ReSUnRegisterPet()

end
-- LogError Tips
function BrokerHandler.OnShowTips(in_state)
    --UISysTips.AddMsg("BrokerHandler.Error  " .. tostring(in_state))
end